package com.controller;

import com.model.UserModel;
import com.services.IUserServices;
import com.services.impl.UserServicesImpl;
import net.sf.json.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Program: exam
 * @ClassName: BlackListServletController
 * @Description: 黑名单
 * @Author: li
 * @Create: 2019-07-23 16:53
 */
@WebServlet("/BlackListServlet")
public class BlackListServletController extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 设置响应格式
        response.setContentType("application/json;charset=UTF-8");
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");

        Map<String, Object> result = new HashMap<>();
        
        try {
            IUserServices services = new UserServicesImpl();
            // 使用 queryAllUser() 方法获取所有用户，然后过滤出黑名单用户
            List<UserModel> allUserModels = services.queryAllUser();
            List<UserModel> blackList = allUserModels.stream()
                .filter(user -> user.getStatus() == -1)  // 假设 status = -1 表示黑名单用户
                .collect(Collectors.toList());

            // 构建返回数据
            result.put("code", 0);
            result.put("msg", "success");
            Map<String, Object> data = new HashMap<>();
            data.put("records", blackList);
            data.put("total", blackList.size());
            result.put("data", data);
            
        } catch (Exception e) {
            result.put("code", 500);
            result.put("msg", "获取黑名单失败：" + e.getMessage());
        }
        
        response.getWriter().write(JSONObject.fromObject(result).toString());
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
